package com.revzone.demo001.PermissionModule.controller;

import com.revzone.demo001.PermissionModule.mapper.UserMapper;
import com.revzone.demo001.PermissionModule.service.UserService;
import com.revzone.demo001.entity.OtherEntity.R;
import com.revzone.demo001.entity.PermissionEntity.UserRoleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * user角色权限controller
 */
@RestController
@Api(tags = "user用户管理", description = "用于user用户管理的相关API")
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;


    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     */
    @ApiOperation(value = "用户角色分页+模糊查询")
    @PostMapping(value="/getUserListPage", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getUserListPage(@RequestBody(required = false) UserRoleDTO userRoleDTO) {
        logger.info("欢迎来到用户分页查询接口!");
        if (userRoleDTO == null) userRoleDTO = new UserRoleDTO();
        return userService.getUserListPage(userRoleDTO);
    }

    @ApiOperation(value = "新增用户数据")
    @PostMapping(value="/setUserList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R setUserList(@RequestBody(required = false) UserRoleDTO userRoleDTO) {
        logger.info("欢迎来到用户新增接口!");
        if (userRoleDTO == null) userRoleDTO = new UserRoleDTO();
        return userService.setUserList(userRoleDTO);
    }

    @ApiOperation(value = "修改用户数据")
    @PostMapping(value="/updateUserList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R updateUserList(@RequestBody(required = false) UserRoleDTO userRoleDTO) {
        logger.info("欢迎来到用户修改接口!");
        if (userRoleDTO == null) userRoleDTO = new UserRoleDTO();
        return userService.updateUserList(userRoleDTO);
    }

    @ApiOperation(value = "删除用户数据")
    @GetMapping(value="/delUserList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R delUserList(@RequestParam(required = false,defaultValue = "0",value = "id") Integer id) {
        logger.info("欢迎来到用户删除接口!");
        return userService.delUserList(id);
    }




















    /**
     * 拥有 vip 权限可以访问该页面
     */
    @GetMapping("/getVipMessage")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    @RequiresPermissions("vip")
    public R getVipMessage() {
        return R.ok("成功获得 vip 信息！");
    }


}
